package web.demo;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class RandomName {
    public static void main(String[] args) throws IOException {
        /*
        * 数组
        * 可以选用io流
        *
        * 随机数
        *
        * */
        String[] studentNameArray = new String[1];
        String nameFile = "./src/web/demo/Name.txt";
        FileInputStream fis =new FileInputStream(nameFile);
        InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
        BufferedReader br =new BufferedReader(isr);
        String line;
        for (int i = 0; i < studentNameArray.length; i++) {
            if ((line=br.readLine()) != null){
                studentNameArray[i] = line;
                studentNameArray = Arrays.copyOf(studentNameArray,studentNameArray.length+1);
            } else{
                studentNameArray = Arrays.copyOf(studentNameArray,studentNameArray.length-1);
                break;
            }
        }
        br.close();

        Scanner scanner = new Scanner(System.in);
        System.out.println("输入抽取人数:");
        int number = scanner.nextInt();
        String[] result = new String[number];

        Random random = new Random();
        for (int i = 0; i < number; i++) {
            int index =random.nextInt(studentNameArray.length-i);
            result[i] = studentNameArray[index];
            for (int j = index; j < studentNameArray.length-i-1; j++) {
                studentNameArray[j]=studentNameArray[j+1];

            }
            
        }

        //System.out.println(Arrays.toString(studentNameArray));

        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i]);
            
        }
    }
}
